#!/bin/bash

find_project_root() {
    local dir="$1"
    while [ "$dir" != "/" ]; do
        if [ -f "$dir/pubspec.yaml" ]; then
            echo "$dir"
            return 0
        fi
        dir=$(dirname "$dir")
    done
    return 1
}

ROOT_DIR=$(find_project_root "$(pwd)") || {
    echo "ERROR: Not in a Flutter project" >&2
    exit 1
}

cd "$ROOT_DIR" || exit 1

if [ ! -f "flutter_launcher_icons.yaml" ]; then
    cat > flutter_launcher_icons.yaml <<EOF
flutter_launcher_icons:
  image_path: "assets/images/logo.png"
  android: true
  ios: true
EOF
    echo "NOTICE: Created default config"
fi

dart run flutter_launcher_icons:generate || {
    echo "ERROR: Generation failed" >&2
    echo "REQUIRED CHECKS:" >&2
    echo "1. Check image path in flutter_launcher_icons.yaml" >&2
    exit 1
}

echo "SUCCESS: App icons updated"